package com.zx.dsptuneup_TriMic.Adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.base.tools.tools.StringUtils;
import com.zx.dsptuneup_TriMic.R;
import com.zx.dsptuneup_TriMic.bean.DeviceBean;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
// 暂时不用
public class AddDeviceAdapter extends BaseAdapter {
    private Context context;
    private List<DeviceBean> gridViewBeanList;
    private SparseBooleanArray mSelectedPositions = new SparseBooleanArray();

    public List<DeviceBean> getGridViewBeanList() {
        return gridViewBeanList;
    }

    public void setGridViewBeanList(List<DeviceBean> gridViewBeanList) {
        this.gridViewBeanList = gridViewBeanList;
        mSelectedPositions = new SparseBooleanArray();
        notifyDataSetChanged();
    }

    public AddDeviceAdapter(Context context, List<DeviceBean> gridViewBeanList) {
        this.context = context;
        this.gridViewBeanList = gridViewBeanList;
    }

    //设置给定位置条目的选择状态
    private void setItemChecked(int position, boolean isChecked) {
        mSelectedPositions.put(position, isChecked);
    }

    /**
     * 全选
     * */
    public void sellectAll() {
        for (int i = 0;i<gridViewBeanList.size(); i++){
            setItemChecked(i,true);
        }
        notifyDataSetChanged();
    }

    /**
     * 取消全选
     * */
    public void cancelSelectAll() {
        for (int i = 0;i<gridViewBeanList.size(); i++){
            setItemChecked(i,false);
        }
        notifyDataSetChanged();
    }

    //根据位置判断条目是否选中
    private boolean isItemChecked(int position) {
        return mSelectedPositions.get(position);
    }
    //获得选中条目的结果
    public ArrayList<DeviceBean> getSelectedItem() {
        ArrayList<DeviceBean> selectList = new ArrayList<>();
        for (int i = 0; i < gridViewBeanList.size(); i++) {
            if (isItemChecked(i)) {
                selectList.add(gridViewBeanList.get(i));
            }
        }
        return selectList;
    }


    @Override
    public int getCount() {
        return gridViewBeanList.size();
    }

    @Override
    public Object getItem(int i) {
        return gridViewBeanList.get(i);
    }


    @Override
    public long getItemId(int i) {
        return i;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        final ViewHolder viewHolder;
        DeviceBean deviceBean = gridViewBeanList.get(i);
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_list_add_device, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }


        ViewGroup.LayoutParams layoutParams = viewHolder.cell_content.getLayoutParams();
        final View itemView = view;

        viewHolder.main_title.setText(StringUtils.isEmpty(deviceBean.getDeviceName()) ? "未知设备名" : deviceBean.getDeviceName());
        viewHolder.sub_title.setText(StringUtils.isEmpty(deviceBean.getPositionName()) ? "未知位置" : deviceBean.getPositionName());

        // status 1 正常 2 忙碌 3 异常 4 掉线
        GradientDrawable gd = (GradientDrawable)viewHolder.ststus_circle.getBackground();
        switch (deviceBean.getStatus()){
            case 1:
                viewHolder.equip_status.setText("已上线");
                gd.setColor(Color.parseColor("#5BCB00"));
                break;
            case 2:
                viewHolder.equip_status.setText("忙碌");
                gd.setColor(Color.parseColor("#486FFF"));
                break;
            case 3:
                viewHolder.equip_status.setText("异常");
                gd.setColor(Color.parseColor("#FEAA00"));
                break;
            case 4:
                viewHolder.equip_status.setText("掉线");
                gd.setColor(Color.parseColor("#D2D2D2"));
                break;
        }


        viewHolder.sel_check.setSelected(isItemChecked(i));

        //条目view的监听
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isItemChecked(i)) {
                    setItemChecked(i, false);
                } else {
                    setItemChecked(i, true);
                }
                notifyDataSetChanged();
            }
        });
        return view;
    }

    static class ViewHolder {
        @BindView(R.id.main_title)
        TextView main_title;
        @BindView(R.id.sub_title)
        TextView sub_title;
        @BindView(R.id.equip_status)
        TextView equip_status;
        @BindView(R.id.ststus_circle)
        TextView ststus_circle;

        @BindView(R.id.dev_rl)
        RelativeLayout dev_rl;

        @BindView(R.id.cell_content)
        RelativeLayout cell_content;

        @BindView(R.id.sel_check)
        TextView sel_check;


        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}